package trading.ctm.yunjing.view

import android.content.Context
import android.content.Intent
import android.graphics.Typeface
import android.support.v4.content.LocalBroadcastManager
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import cc.vv.baselibrary.util.DensityUtil
import cc.vv.lkbasecomponent.util.LKSPUtil
import kotlinx.android.synthetic.main.view_home_auction.view.*

import trading.ctm.yunjing.R

/**
 * @desc 首页拍卖
 * @author zouxiaorui
 * @date  2018/8/21
 */
class HomeAuctionView : LinearLayout {
    constructor(context: Context) : super(context) {
        initView()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initView()
    }

    private fun initView() {
        val view = View.inflate(context, R.layout.view_home_auction, null)
        val tv_vha_auction_check_more = view.findViewById(R.id.tv_vha_auction_check_more) as TextView
        //查看更多
        tv_vha_auction_check_more.setOnClickListener {
            val intent = Intent("fragment")
            intent.putExtra("fragment_key", 2)
            LKSPUtil.getInstance().put("isTableHost", 0)
            LocalBroadcastManager.getInstance(context).sendBroadcast(intent)
        }
        this.addView(view)
    }


    fun initData(mData: List<String>) {
        clearnLinView()
        val lp = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, DensityUtil.dip2px(48f))
        for (i in 0 until mData.size) {
            val view = View.inflate(context, R.layout.view_hav_auction, null)
            view.layoutParams = lp
            val tv_ifna_medicine_name = view.findViewById<TextView>(R.id.tv_ifna_medicine_name)
            val tv_ifna_medicine_field = view.findViewById<TextView>(R.id.tv_ifna_medicine_field)
            val tv_ifna_starting_prasing = view.findViewById<TextView>(R.id.tv_ifna_starting_prasing)
            val tv_ifna_new_prasing = view.findViewById<TextView>(R.id.tv_ifna_new_prasing)
            val tv_ifna_product_guego = view.findViewById<TextView>(R.id.tv_ifna_product_guego)
            val tv_ifna_surplus = view.findViewById<TextView>(R.id.tv_ifna_surplus)

            if (i % 2 == 0) {
                view.setBackgroundResource(R.color.color_FBF7F5)
            } else {
                view.setBackgroundResource(R.color.color_FFFFFF)
            }
            setTextType(tv_ifna_starting_prasing)
            setTextType(tv_ifna_new_prasing)
            setTextType(tv_ifna_surplus)

            tv_ifna_medicine_name.text = mData[i]
            tv_ifna_medicine_field.text = mData[i]
            tv_ifna_starting_prasing.text = mData[i]
            tv_ifna_new_prasing.text = mData[i]
            tv_ifna_product_guego.text = mData[i]
            tv_ifna_surplus.text = mData[i]
            ll_vha_auction_list.addView(view)
        }

    }

    private fun clearnLinView() {
        ll_vha_auction_list?.removeAllViews()
    }

    private fun setTextType(tv: TextView){
        var TEXT_TYPE: Typeface? = null
        try {
            // 加载自定义字体
            TEXT_TYPE = Typeface.createFromAsset(context.assets, "fonts/BebasNeue.TTF")
        } catch (e: Exception) {
            TEXT_TYPE = null
        }

        if(null != TEXT_TYPE){
            tv.typeface = TEXT_TYPE
        }
    }


}
